/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoProducto.java
 *
 * Created on 19/05/2010, 11:35:33 AM
 */

package ui;

import manejoExcepciones.ExcepcionesInventario;
import acceso.Producto;
import gestion.GestionSpring;
import interfaz.InterfaceEjp;
import javax.swing.JOptionPane;

/**
 *Clase donde se realiza la insercion de un nuevo producto
 *
 * @author Adriano
 */
public class NuevoProducto extends javax.swing.JFrame {


    PaginaPrincipal miPaginaPrincipal;

    public void setMiPaginaPrincipal(PaginaPrincipal miPaginaPrincipal) {
        this.miPaginaPrincipal = miPaginaPrincipal;
    }


    /** Creates new form NuevoProducto */
    public NuevoProducto() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        NombreI = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TipoCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        DescripcionI = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        CantidadI = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        PrecioI = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        PresentacionI = new javax.swing.JTextField();
        BotonOkInsertar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Mensaje = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inversiones E.J.P");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nuevo Producto"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("(*)Nombre:");
        jLabel1.setName("jLabel1"); // NOI18N

        NombreI.setName("NombreI"); // NOI18N

        jLabel2.setText("(*)Tipo:");
        jLabel2.setName("jLabel2"); // NOI18N

        TipoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Papeleria", "Limpieza" }));
        TipoCombo.setName("TipoCombo"); // NOI18N

        jLabel3.setText("Descripcion:");
        jLabel3.setName("jLabel3"); // NOI18N

        DescripcionI.setName("DescripcionI"); // NOI18N

        jLabel4.setText("(*)Cantidad:");
        jLabel4.setName("jLabel4"); // NOI18N

        CantidadI.setName("CantidadI"); // NOI18N

        jLabel5.setText("(*)Precio:");
        jLabel5.setName("jLabel5"); // NOI18N

        PrecioI.setName("PrecioI"); // NOI18N

        jLabel6.setText("(*)Presentacion:");
        jLabel6.setName("jLabel6"); // NOI18N

        PresentacionI.setName("PresentacionI"); // NOI18N

        BotonOkInsertar.setText("OK");
        BotonOkInsertar.setName("BotonOkInsertar"); // NOI18N
        BotonOkInsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonOkInsertarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        Mensaje.setName("Mensaje"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(BotonOkInsertar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Mensaje, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(PrecioI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(CantidadI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(DescripcionI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(NombreI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(TipoCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, 198, Short.MAX_VALUE)
                            .addComponent(PresentacionI, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))
                        .addGap(20, 20, 20))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NombreI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(TipoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(DescripcionI, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CantidadI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(PrecioI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(PresentacionI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Mensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonOkInsertar)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-352)/2, (screenSize.height-378)/2, 352, 378);
    }// </editor-fold>//GEN-END:initComponents

    private void BotonOkInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonOkInsertarActionPerformed

       //Instanciacion de la interface.
       InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();
       //Verificamos si se a seleccionado en el combobox el tipo de producto
       //de papeleria o limpieza.
       int Seleccion =  TipoCombo.getSelectedIndex();
       char TipoI = 0;
       if (Seleccion == 0)
           TipoI = 'P';
       else if (Seleccion == 1)
           TipoI = 'L';

           try{
               //Verificamos si los campos obligatorios han sido llenados.
               manejoExcepciones.ExcepcionesInventario.CamposNoLlenos
               (NombreI.getText(), PrecioI.getText(),
               CantidadI.getText(), PresentacionI.getText());

               //Verificamos que Precio y Cantidad sean valores numericos.
               float Precio = Float.parseFloat(PrecioI.getText());
               int   Cant   = Integer.parseInt(CantidadI.getText());

               //Verificamos que Precio y Cantidad sean >= 0 (Cero)
               manejoExcepciones.ExcepcionesInventario.
               CamposNoNegativos(Precio, Cant);

               //Construimos el objeto producto para ingresarlo a la BD.
               Producto miProducto = new Producto(NombreI.getText(), TipoI ,
               DescripcionI.getText() ,Float.parseFloat(PrecioI.getText()) ,
               Integer.parseInt(CantidadI.getText()),PresentacionI.getText());
               //Registramos el producto.
               Long a = miInterfaceEjp.registrarProducto(miProducto);
               //Enviamos mensaje de exito.
               JOptionPane.showMessageDialog(this,"Insercion Exitosa");
               //Se refesca el JTable TablaProducto.
               miPaginaPrincipal.Refrescar();
               //Se oculta la pagina de NuevoProducto.
               this.setVisible(false);

           }catch (ExcepcionesInventario ExI1){

               JOptionPane.showMessageDialog(this,ExI1.getMessage());

           }catch (NumberFormatException NFEI){

               JOptionPane.showMessageDialog(this,"Los campos cantidad y precio deben ser numericos");
           }

        

       


}//GEN-LAST:event_BotonOkInsertarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NuevoProducto().setVisible(true);
            }
        });
    }


     





    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonOkInsertar;
    private javax.swing.JTextField CantidadI;
    private javax.swing.JTextField DescripcionI;
    private javax.swing.JLabel Mensaje;
    private javax.swing.JTextField NombreI;
    private javax.swing.JTextField PrecioI;
    private javax.swing.JTextField PresentacionI;
    private javax.swing.JComboBox TipoCombo;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
